<%@page contentType="text/html;charset=UTF-8" %>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="service.BOService"%>
<%@page import="service.ORMService"%>
<%@page import="tools.*" %>
<%@page import="bo.*" %>
<%@page import="java.text.*" %>
<%@page import="java.util.*" %> 


<%

	request.setCharacterEncoding("utf-8");
	String action = request.getParameter("ACTION");
	ORMService ormService = ORMService.getInstance();
	BOService boService = BOService.getInstance();
	if(action.equals("addArticle")) {
		String id = DepUtil.createPrimaryId();
		String title = request.getParameter("TITLE");
		String content = request.getParameter("CONTENT");
		String articleTypeId = request.getParameter("ARTICLE_TYPE_ID");
		BusinessObject bo = boService.createBusinessObject("Article");
		bo.put("ID", id);
		bo.put("TITLE", title);
		bo.put("CONTENT", content);
		bo.put("ARTICLE_TYPE_ID", articleTypeId);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		bo.put("CREATE_TIME", format.format(new Date()));
		ormService.insert(bo);
		String sql = "select * from T_BLOG_ARTICLE where ID = '" + id + "'";
		bo = ormService.get(sql, bo);
		response.getWriter().write(bo.getJsonString());
	} else if(action.equals("editArticle")) {
		String id = request.getParameter("ID");
		String title = request.getParameter("TITLE");
		String content = request.getParameter("CONTENT");
		String articleTypeId = request.getParameter("ARTICLE_TYPE_ID");
		BusinessObject bo = boService.createBusinessObject("Article");
		bo = ormService.get(bo, id);
		bo.put("TITLE", title);
		bo.put("CONTENT", content);
		ormService.update(bo);
		String sql = "select * from T_BLOG_ARTICLE where ID = '" + id + "'";
		bo = ormService.get(sql, bo);
		response.getWriter().write(bo.getJsonString());
	} else if(action.equals("deleteArticle")) {
		String flag = "";
		try {
			String id = request.getParameter("ID");
			String sql = "delete from T_BLOG_ARTICLE where ID = '" + id + "'";
			ormService.execute(sql, null);
			flag = "{\"FLAG\":\"1\"}";
		} catch(Exception e) {
			e.printStackTrace();
			flag = "{\"FLAG\":\"0\"}";
		}
		response.getWriter().write(flag);
	} else if(action.equals("searchArticlesByType")) {
		String articleTypeId = request.getParameter("ARTICLE_TYPE_ID");
		String sql = "select * from T_BLOG_ARTICLE where ARTICLE_TYPE_ID = '" + articleTypeId + "' order by CREATE_TIME DESC";
		List<BusinessObject> articleTypeList = ormService.list(sql);
		JSONArray jsonArr = DepUtil.toJSONArray(articleTypeList);
		response.getWriter().write(jsonArr.toString());
	} else if(action.equals("searchArticleById")) {
		String articleId = request.getParameter("ARTICLE_ID");
		BusinessObject bo = boService.createBusinessObject("Article");
		bo = ormService.get(bo, articleId);
		response.getWriter().write(bo.getJsonString());
	} else if(action.equals("addArticleType")) {
		String id = DepUtil.createPrimaryId();
		String name = request.getParameter("NAME");
		BusinessObject bo = boService.createBusinessObject("ArticleType");
		bo.put("ID", id);
		bo.put("NAME", name);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		bo.put("CREATE_TIME", format.format(new Date()));
		ormService.insert(bo);
		String sql = "select * from T_BLOG_ARTICLE_TYPE where ID = '" + id + "'";
		bo = ormService.get(sql, bo);
		response.getWriter().write(bo.getJsonString());
	} else if(action.equals("doLoadArticleType")) {
		String sql = "select * from T_BLOG_ARTICLE_TYPE order by CREATE_TIME DESC";
		List<BusinessObject> articleTypeList = ormService.list(sql);
		JSONArray jsonArr = DepUtil.toJSONArray(articleTypeList);
		response.getWriter().write(jsonArr.toString());
	}
%>